import React from "react";
import ReactDOM from "react-dom";
import {HashRouter as Router, Route, Switch, Redirect} from "react-router-dom"
import {Provider} from "mobx-react";
import "./style";
import allStore from "./stores";
import {asyncComponent} from "react-async-component";
import {Tools} from "weaver-mobile";
import * as API from "./api/cptApi"

const getLocaleLabel = Tools.getLocaleLabel.bind(this, "cpt_mobile");
// const getLabel = Tools.getLabel;


const coms = {
    Home: asyncComponent({
        resolve: () => import("./components/main")
    }),
    Index: asyncComponent({
        resolve: () => import("./components/index")
    }),
    Acceptance: asyncComponent({
        resolve: () => import("./components/asset/acceptance")
    }),
    AcceptanceDetail: asyncComponent({
        resolve: () => import("./components/asset/acceptanceDetail")
    }),
    AssetInfo: asyncComponent({
        resolve: () => import("./components/asset/assetInfo")
    }),
    Operations: asyncComponent({
        resolve: () => import("./components/asset/operations")
    }),
    Report: asyncComponent({
        resolve: () => import("./components/statistics/report")
    }),
    InventoryForm: asyncComponent({
        resolve: () => import("./components/inventory/inventoryForm")
    }),
    InventoryDetail: asyncComponent({
        resolve: () => import("./components/inventory/inventoryDetail")
    }),
    MyAssetPage: asyncComponent({
        resolve: () => import("./components/singlePage/myAssetPage")
    }),

};

window.cpt_page_right = {};

class Root extends React.Component {
    getLocaleLabel = Tools.getLocaleLabel;

    constructor(props) {
        super(props);
        this.state = {
            loadLang: false
        };
        this.getLocaleLabel.bind(this);
    }

    componentDidMount() {
    /*    Tools.ssoInit().then(() => {
            this.getLocaleLabel("cpt_mobile", () => {
                this.setState({loadLang: true});

            });

        }).catch((res) => {
            let reason = "";
            if (JSON.stringify(res) !== "{}") {
                reason = "," + res;
            }
            Toast.fail(getLabel(386481, "登录失败") + reason, 0);
        });*/

        API.getCptMenuRight().then(res => {
            window.cpt_page_right = res;
        });
    }

    render() {
        return (
            <Provider {...allStore}>
                <Router>
                    <Route name="main" path="/" onEnter={getLocaleLabel} render={() => {
           /*             if (!this.state.loadLang) {
                            return <div></div>
                        }*/
                        return (
                            <coms.Home>
                                <Switch>
                                    <Route name="Index" path="/index/:tab" component={coms.Index}/>
                                    <Route name="Acceptance" path="/acceptance/:path" component={coms.Acceptance}/>
                                    <Route name="AcceptanceDetail" path="/acceptanceDetail/:id"
                                           component={coms.AcceptanceDetail}/>
                                    <Route name="AssetInfo" path="/assetInfo/:path/:id" component={coms.AssetInfo}/>
                                    <Route name="Operations" path="/operations/:path" component={coms.Operations}/>
                                    <Route name="Report" path="/report/:path" component={coms.Report}/>
                                    <Route name="MyAssetPage" path="/myassetsingle" component={coms.MyAssetPage}/>
                                    <Route name="InventoryForm" path="/inventoryForm/:id/:cptid/:viewAttr"
                                           component={coms.InventoryForm}/>
                                    <Route name="InventoryDetail" path="/inventoryDetail/:from/:inventoryId"
                                           component={coms.InventoryDetail}/>
                                    <Redirect exact path="/" to={{pathname: "/index/home"}}/>
                                </Switch>
                            </coms.Home>
                        )

                    }
                    }/>
                </Router>
            </Provider>
        )
    }
}

// ReactDOM.render(<Root/>, document.getElementById("root"));
Tools.getLocaleLabel("cpt_mobile", () => {
    ReactDOM.render(<Root/>, document.getElementById("root"))
})
